<?php
class Comment extends AppModel {
	var $name = 'Comment';
	var $validate = array(
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'design_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Design' => array(
			'className' => 'Design',
			'foreignKey' => 'design_id',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'counterCache' => true
		)
	);
	
	var $order = 'Comment.created ASC';
	
	var $actsAs = array('Containable');
	
	// get comment updates
	function get_updates() {
		// check id user is contractor
		if ($_SESSION['Auth']['User']['group_id'] == 2) {
			$comments = array();
			// get all pitches that belong to user
			$pitches = $this->Design->Pitch->get_pitches();
			// get all comments for designs related to pitches that belong to user
			foreach($pitches as $pitch) {
				foreach($pitch['Design'] as $design) {
					$conditions = array(
						// make sure comments by user self are not returned
						'NOT' => array(
							'Comment.user_id' => $_SESSION['Auth']['User']['id'],
						),
						'Comment.design_id' => $design['id']
					);
					$c = $this->find('all', array('conditions' => $conditions, 'order' => array('Comment.created' => 'DESC'), 'limit' => 4, 'contain' => array('User' => array('ContractorProfile', 'DesignerProfile'), 'Design')));
					$comments = array_merge($comments, $c);
				}
			}
			// sort all comments
			$comments = $this->sort_comments($comments);
			return $comments;
		} else {
			// if user is designer
			$comments = array();
			// find all designs that belong to user
			$designs = $this->Design->findMy('all');
			// find all comments for designs that belong to user
			foreach($designs as $design) {
				$conditions = array(
					// make sure comments by user self are not returned
					'NOT' => array(
						'Comment.user_id' => $_SESSION['Auth']['User']['id']
					),
					'Comment.design_id' => $design['Design']['id']
				);
				$c = $this->find('all', array('conditions' => $conditions, 'order' => array('Comment.created' => 'DESC'), 'limit' => 4, 'contain' => array('User' => array('ContractorProfile', 'DesignerProfile'), 'Design')));
				$comments = array_merge($comments, $c);
			}
			// sort comments
			$comments = $this->sort_comments($comments);
			return $comments;
		}
	}
	
	// function to sort comments
	function sort_comments($data) {
		$tmp = array();
		// set order
		foreach($data as &$c)
			$tmp[] = &$c['Comment']['created'];
		// sort array
		array_multisort($tmp, $data);
		$comments = array_reverse($data);
		return $comments;
	}
		
}
?>
